package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.PageInfo;
import com.genesis.mall.entity.PmsProduct;
import com.genesis.mall.entity.SmsFlashPromotion;
import com.genesis.mall.entity.SmsFlashPromotionProductRelation;
import com.genesis.mall.entity.SmsFlashPromotionSession;
import com.genesis.mall.mapper.SmsFlashPromotionProductRelationMapper;
import com.genesis.mall.service.IPmsProductService;
import com.genesis.mall.service.ISmsFlashPromotionProductRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genesis.mall.service.ISmsFlashPromotionService;
import com.genesis.mall.service.ISmsFlashPromotionSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品限时购与商品关系表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@Service
public class SmsFlashPromotionProductRelationServiceImpl extends ServiceImpl<SmsFlashPromotionProductRelationMapper, SmsFlashPromotionProductRelation> implements ISmsFlashPromotionProductRelationService {
   @Autowired
    private ISmsFlashPromotionService smsFlashPromotionService;
    @Autowired
    private ISmsFlashPromotionSessionService smsFlashPromotionSessionService;
    @Autowired
    private IPmsProductService pmsProductService;

    //批量选择商品添加关联
    @Override
    public void create(List<SmsFlashPromotionProductRelation> smss) {
        for (SmsFlashPromotionProductRelation sfpr : smss) {
            LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SmsFlashPromotionProductRelation::getId,sfpr.getId());
            List<SmsFlashPromotionProductRelation> list = this.list(queryWrapper);
            List<SmsFlashPromotionProductRelation> collect = list.stream().map(item -> {
                item.setSort(sfpr.getSort());
                item.setFlashPromotionId(sfpr.getFlashPromotionSessionId());
                item.setProductId(sfpr.getProductId());
                item.setFlashPromotionPrice(sfpr.getFlashPromotionPrice());
                item.setFlashPromotionLimit(sfpr.getFlashPromotionLimit());
                item.setFlashPromotionSessionId(sfpr.getFlashPromotionSessionId());
                return item;
            }).collect(Collectors.toList());
            this.updateBatchById(list);

        }
    }

    //修改
    @Override
    public void updateBy(SmsFlashPromotionProductRelation sms) {
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsFlashPromotionProductRelation::getId,sms.getId());
        List<SmsFlashPromotionProductRelation> list = this.list(queryWrapper);
        List<SmsFlashPromotionProductRelation> collect = list.stream().map(item -> {
            item.setFlashPromotionId(sms.getFlashPromotionId());
            item.setFlashPromotionCount(sms.getFlashPromotionCount());
            item.setFlashPromotionPrice(new BigDecimal(sms.getFlashPromotionPrice().doubleValue()));
            item.setFlashPromotionLimit(sms.getFlashPromotionLimit());
            item.setProductId(sms.getProductId());
            item.setFlashPromotionSessionId(sms.getFlashPromotionSessionId());
            item.setSort(sms.getSort());
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(collect);
    }
//分页
    @Override
    public PageInfo listAll(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        SmsFlashPromotion smsFlashPromotion = smsFlashPromotionService.getById(flashPromotionId);
        String title = smsFlashPromotion.getTitle();
        SmsFlashPromotionSession smsFlashPromotionSession = smsFlashPromotionSessionService.getById(flashPromotionSessionId);
        String name = smsFlashPromotionSession.getName();
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        Page<PmsProduct> objectPage= new Page<>(pageNum, pageSize);
        Page<PmsProduct> page = pmsProductService.page(objectPage, queryWrapper);
        List<PmsProduct> records = page.getRecords();
        int total = (int)page.getTotal();
        int current = (int)page.getCurrent();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(records);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotalPage(current);
        return pageInfo;
    }
}
